//
//  LSZLyricBooksLyricsVC.m
//  JKFuYinProject
//
//  Created by 九酷 on 2018/8/4.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LSZLyricBooksLyricsVC.h"
#import "TYCyclePagerViewCell.h"

#import "TYCyclePagerView.h"
#import "LSZLyricBookModel.h"

@interface LSZLyricBooksLyricsVC ()<TYCyclePagerViewDataSource,TYCyclePagerViewDelegate>

@property (nonatomic, strong) TYCyclePagerView *pagerView;
@property (nonatomic, strong) UIView * viewLow;
@property (nonatomic, strong) NSString * curIndexChange;

@property (nonatomic, strong) UIButton * butUseThis;
@end

@implementation LSZLyricBooksLyricsVC

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.curIndexChange = @"-1";
    [self addPagerView];
    [self addThreebutton];
    [self setLyricBooksLyricsUI];
    [self useThis];
}
-(void)useThis
{
    self.butUseThis = [[UIButton alloc]init];
    self.butUseThis.frame = CGRectMake(20, self.view.frame.size.height-70, ScreenWidth-40, 55);
    [self.butUseThis.layer setMasksToBounds:YES];
    [self.butUseThis.layer setCornerRadius:55/2];
    self.butUseThis.backgroundColor = redUIColor;
    self.butUseThis.userInteractionEnabled = YES;
    [self.butUseThis setTitle:@"使用" forState:UIControlStateNormal];
    [self.butUseThis addTarget:self action:@selector(clickButUseThisNew) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.butUseThis];
}
-(void)clickButUseThisNew
{
    LSZLyricBookModel * model = [[LSZLyricBookModel alloc]init];
    NSArray * arrayExisting = [WHCSqlite query:[model class]];
    NSString * string = @"0";
    NSString * imageStr = @"";
    for ( LSZLyricBookModel * mg in arrayExisting) {
        if ([mg.id_id isEqualToString:self.id_id]) {
            string = @"1";
            imageStr = [NSString stringWithFormat:@"%@",mg.img];
            break;
        }
    }
    if ([string isEqualToString:@"1"]) {
        NSLog(@"有数据");
    }else{
   #pragma mark - 没有歌谱
        LSZLyricBooksMg * manager = nil;
        manager = self.arrayManager[self.curIndex];
        model.id_id = self.id_id;
        model.author = manager.author;
        model.name = manager.name;
        model.album = manager.album;
        model.img = manager.img;
        model.url = manager.url;
        [WHCSqlite insert:model];
        self.butUseThis.userInteractionEnabled = NO;
        self.butUseThis.backgroundColor = lightColor;
        [self.butUseThis setTitle:@"已选择" forState:UIControlStateNormal];
    }
}
- (void)addPagerView {
    TYCyclePagerView *pagerView = [[TYCyclePagerView alloc]init];
    pagerView.dataSource = self;
    pagerView.delegate = self;
    pagerView.autoScrollInterval = 3.0;
    pagerView.isInfiniteLoop = NO;
    [pagerView registerClass:[TYCyclePagerViewCell class] forCellWithReuseIdentifier:@"cellId"];
    [self.view addSubview:pagerView];
    _pagerView = pagerView;
    _pagerView.frame = CGRectMake(0,10 + NAVIGATION_BAR_HEIGHT, CGRectGetWidth(self.view.frame),self.view.frame.size.height-NAVIGATION_BAR_HEIGHT-10-80);

//    _pagerView.frame = CGRectMake(0,10, ScreenWidth,self.view.frame.size.height-40);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSInteger changIntValue = self.curIndex;
        if (changIntValue != 0) {
            UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.pagerView.collectionView.collectionViewLayout;
            CGFloat leftEdge = layout.sectionInset.left;
             CGFloat width = CGRectGetWidth(self.pagerView.collectionView.frame);
            CGFloat itemWidth = layout.itemSize.width + layout.minimumInteritemSpacing;
            CGFloat offsetX = leftEdge + itemWidth*self.curIndex - layout.minimumInteritemSpacing/2 - (width - itemWidth)/2;
            if (ScreenWidth == 320) {
                //5
                [self.pagerView.collectionView setContentOffset:CGPointMake(offsetX+35, self.pagerView.collectionView.contentOffset.y) animated:YES];
            }else {
                //plus
                [self.pagerView.collectionView setContentOffset:CGPointMake(offsetX+44, self.pagerView.collectionView.contentOffset.y) animated:YES];
             }

            self.curIndexChange = @"0";
//            [self pagerView:self.pagerView cellForItemAtIndex:self.curIndex];
        }else{
            self.curIndexChange = @"0";
        }
    });
    [_pagerView setNeedUpdateLayout];

}
#pragma mark - TYCyclePagerViewDataSource
- (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView {
    return self.arrayManager.count;
}
- (UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index {
    TYCyclePagerViewCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndex:index];
    cell.imageViewBack.tag = index+1;
    LSZLyricBooksMg * manager = nil;
    manager = self.arrayManager[index];
    [cell.imageViewBack sd_setImageWithURL:[NSURL URLWithString:manager.img]];
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}
- (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView {
    TYCyclePagerViewLayout *layout = [[TYCyclePagerViewLayout alloc]init];
    layout.itemSize = CGSizeMake(CGRectGetWidth(pageView.frame)*0.8, CGRectGetWidth(pageView.frame)*1.6*0.8);
    layout.itemSpacing = 15;
    layout.layoutType = 1;
    layout.itemHorizontalCenter = YES;
    return layout;
}
- (void)pagerView:(TYCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    if ([self.curIndexChange isEqualToString:@"-1"]) {
        NSLog(@"111111");
    }else{
        if (![self.curIndexChange isEqualToString:@"-1"]) {
            self.curIndex = toIndex;
        }
    }
}
#pragma mark - 点击的是第几个
- (void)pagerView:(TYCyclePagerView *)pageView didSelectedItemCell:(__kindof UICollectionViewCell *)cell atIndex:(NSInteger)index
{
    //点击图片保存到相册  不要了 隐藏
    //    for (UIView *tmpView in cell.subviews ){
    //        if([tmpView isMemberOfClass:[UIImageView class]]){
    //            UIImageView * imageView = (UIImageView *)tmpView;
    //            NSLog(@"%@",imageView.image);
    //            if (imageView.tag == index+1) {
    //                UIImage * saveimg=[self saveImage:imageView];
    //                UIImageWriteToSavedPhotosAlbum(saveimg, self, @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
    //            }
    //        }
    //    }
}
-(void)clickOneBut:(UIButton *)but
{
    if(but.tag == 1){
        if (self.curIndex != 0) {
            //滚动到前一个选项
//            [self.pagerView scrollToNearlyIndexAtDirection:TYPagerScrollDirectionLeft animate:YES];
            self.curIndex = self.curIndex-1 ;
            [self numberCurIndex];
        }
    }else{
        //滚动到后一个选项
        if (self.curIndex != self.arrayManager.count-1) {
            self.curIndex = self.curIndex+1;
            [self numberCurIndex];
//            [self.pagerView scrollToNearlyIndexAtDirection:TYPagerScrollDirectionRight animate:YES];
        }
    }
}
-(void)numberCurIndex
{
    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.pagerView.collectionView.collectionViewLayout;
    CGFloat leftEdge = layout.sectionInset.left;
    CGFloat width = CGRectGetWidth(self.pagerView.collectionView.frame);
    CGFloat itemWidth = layout.itemSize.width + layout.minimumInteritemSpacing;
    CGFloat offsetX = leftEdge + itemWidth*self.curIndex - layout.minimumInteritemSpacing/2 - (width - itemWidth)/2;
    if (ScreenWidth == 320) {
        [self.pagerView.collectionView setContentOffset:CGPointMake(offsetX+35, self.pagerView.collectionView.contentOffset.y) animated:YES];
    }else {
        [self.pagerView.collectionView setContentOffset:CGPointMake(offsetX+44, self.pagerView.collectionView.contentOffset.y) animated:YES];
    }
}
-(void)setLyricBooksLyricsUI
{
    UIView * viewTop = [[UIView alloc]init];
    viewTop.frame = CGRectMake(0, 0, ScreenWidth, NAVIGATION_BAR_HEIGHT);
    viewTop.backgroundColor = redUIColor;
    [self.view addSubview:viewTop];

    UIButton * butCancel = [[UIButton alloc]init];
    [butCancel setImage:[UIImage imageNamed:@"白叉号"] forState:UIControlStateNormal];
    butCancel.frame = CGRectMake(0, STATUS_BAR_HEIGHT, 60, NAVIGATION_BAR_HEIGHT-STATUS_BAR_HEIGHT);
    [butCancel addTarget:self action:@selector(clickCancel) forControlEvents:UIControlEventTouchUpInside];
    [viewTop addSubview:butCancel];

    UILabel * labelTitle = [[UILabel alloc]init];
    labelTitle.textColor = [UIColor whiteColor];
    labelTitle.text = @"歌谱搜索";
    labelTitle.font = FONT_20;
    labelTitle.textAlignment = NSTextAlignmentCenter;
    labelTitle.frame = CGRectMake(CGRectGetMaxX(butCancel.frame), STATUS_BAR_HEIGHT, ScreenWidth-CGRectGetMaxX(butCancel.frame)*2, NAVIGATION_BAR_HEIGHT-STATUS_BAR_HEIGHT);
    [viewTop addSubview:labelTitle];
}
-(void)addThreebutton
{
    UIButton * butOne = [[UIButton alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height/2, 50, 50)];
    [butOne setBackgroundImage:[UIImage imageNamed:@"向左"] forState:UIControlStateNormal];
    butOne.tag = 1;
    [butOne addTarget:self action:@selector(clickOneBut:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:butOne];

    UIButton * butTwo = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width-50, self.view.frame.size.height/2, 50,50)];
    [butTwo setBackgroundImage:[UIImage imageNamed:@"向右"] forState:UIControlStateNormal];
    butTwo.tag = 2;
    [butTwo addTarget:self action:@selector(clickOneBut:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:butTwo];
}
#pragma mark - 取消按钮
-(void)clickCancel
{
    [self dismissViewControllerAnimated:YES completion:NULL];
}
@end
